﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Ghostify {
	public partial class ViewWindow : Form {
		public int locks = 5;
		public double lives = 5;
		public bool is_protected = false;
		public bool game_over = false;

		public double karma = 10.00;
		public int score = 0;
		public int points = 0;

		public ViewWindow() {
			InitializeComponent();
			SetSprites();
			UpdateLives();
			UpdateLocks();
		}

		public void SetSprites() {
			((Bitmap)this.ghost.BackgroundImage).MakeTransparent(Color.Black);
		}

		public void UseLock() {
			if (locks > 0 && !is_protected) {
				locks--;
				is_protected = true;
				Console.Beep(700, 75);
				Console.Beep(800, 100);
				UpdateLocks();
			} else {
				Console.Beep(400, 75);
				Console.Beep(300, 100);
			}
		}

		public void UpdateLives() {
			if (lives == 5) {
				heart1.BackgroundImage = Properties.Resources.heart;
				heart2.BackgroundImage = Properties.Resources.heart;
				heart3.BackgroundImage = Properties.Resources.heart;
				heart4.BackgroundImage = Properties.Resources.heart;
				heart5.BackgroundImage = Properties.Resources.heart;
			} else if (lives == 4.5) {
				heart1.BackgroundImage = Properties.Resources.heart;
				heart2.BackgroundImage = Properties.Resources.heart;
				heart3.BackgroundImage = Properties.Resources.heart;
				heart4.BackgroundImage = Properties.Resources.heart;
				heart5.BackgroundImage = Properties.Resources.halfheart;
			} else if (lives == 4) {
				heart1.BackgroundImage = Properties.Resources.heart;
				heart2.BackgroundImage = Properties.Resources.heart;
				heart3.BackgroundImage = Properties.Resources.heart;
				heart4.BackgroundImage = Properties.Resources.heart;
				heart5.BackgroundImage = Properties.Resources.noheart;
			} else if (lives == 3.5) {
				heart1.BackgroundImage = Properties.Resources.heart;
				heart2.BackgroundImage = Properties.Resources.heart;
				heart3.BackgroundImage = Properties.Resources.heart;
				heart4.BackgroundImage = Properties.Resources.halfheart;
				heart5.BackgroundImage = Properties.Resources.noheart;
			} else if (lives == 3) {
				heart1.BackgroundImage = Properties.Resources.heart;
				heart2.BackgroundImage = Properties.Resources.heart;
				heart3.BackgroundImage = Properties.Resources.heart;
				heart4.BackgroundImage = Properties.Resources.noheart;
				heart5.BackgroundImage = Properties.Resources.noheart;
			} else if (lives == 2.5) {
				heart1.BackgroundImage = Properties.Resources.heart;
				heart2.BackgroundImage = Properties.Resources.heart;
				heart3.BackgroundImage = Properties.Resources.halfheart;
				heart4.BackgroundImage = Properties.Resources.noheart;
				heart5.BackgroundImage = Properties.Resources.noheart;
			} else if (lives == 2) {
				heart1.BackgroundImage = Properties.Resources.heart;
				heart2.BackgroundImage = Properties.Resources.heart;
				heart3.BackgroundImage = Properties.Resources.noheart;
				heart4.BackgroundImage = Properties.Resources.noheart;
				heart5.BackgroundImage = Properties.Resources.noheart;
			} else if (lives == 1.5) {
				heart1.BackgroundImage = Properties.Resources.heart;
				heart2.BackgroundImage = Properties.Resources.halfheart;
				heart3.BackgroundImage = Properties.Resources.noheart;
				heart4.BackgroundImage = Properties.Resources.noheart;
				heart5.BackgroundImage = Properties.Resources.noheart;
			} else if (lives == 1) {
				heart1.BackgroundImage = Properties.Resources.heart;
				heart2.BackgroundImage = Properties.Resources.noheart;
				heart3.BackgroundImage = Properties.Resources.noheart;
				heart4.BackgroundImage = Properties.Resources.noheart;
				heart5.BackgroundImage = Properties.Resources.noheart;
			} else if (lives == 0.5) {
				heart1.BackgroundImage = Properties.Resources.halfheart;
				heart2.BackgroundImage = Properties.Resources.noheart;
				heart3.BackgroundImage = Properties.Resources.noheart;
				heart4.BackgroundImage = Properties.Resources.noheart;
				heart5.BackgroundImage = Properties.Resources.noheart;
			} else if (lives <= 0) {
				heart1.BackgroundImage = Properties.Resources.noheart;
				heart2.BackgroundImage = Properties.Resources.noheart;
				heart3.BackgroundImage = Properties.Resources.noheart;
				heart4.BackgroundImage = Properties.Resources.noheart;
				heart5.BackgroundImage = Properties.Resources.noheart;

				GameOver();
			}
		}

		public void GameOver() {
			Beep.volume = 45;
			Beep.duration += 50;
			game_over = true;

			foreach (Control control in viewport.Controls) {
				viewport.Controls.Remove(control);
			}

			Console.Beep(600, 300);
			Console.Beep(500, 400);
			Console.Beep(400, 500);
			Console.Beep(300, 600);

			viewport.BackgroundImage = Properties.Resources.gameover;

			entity_spawner.Enabled = false;
			protection_timer.Enabled = false;
			entity_updater_timer.Enabled = false;
		}

		public void UpdateLocks() {
			lock1.BackgroundImage = (locks >= 1) ? Properties.Resources._lock : Properties.Resources.nolock;
			lock2.BackgroundImage = (locks >= 2) ? Properties.Resources._lock : Properties.Resources.nolock;
			lock3.BackgroundImage = (locks >= 3) ? Properties.Resources._lock : Properties.Resources.nolock;
			lock4.BackgroundImage = (locks >= 4) ? Properties.Resources._lock : Properties.Resources.nolock;			lock1.BackgroundImage = (locks >= 1) ? Properties.Resources._lock : Properties.Resources.nolock;
			lock5.BackgroundImage = (locks >= 5) ? Properties.Resources._lock : Properties.Resources.nolock;

			if (is_protected) {
				ghost.BackgroundImage = Properties.Resources.ghost_protected;
				protection_timer.Enabled = true;
			} else {
				ghost.BackgroundImage = Properties.Resources.ghost;
				protection_timer.Enabled = false;
			}

			((Bitmap)ghost.BackgroundImage).MakeTransparent(Color.Black);
		}

		protected override void OnKeyDown(KeyEventArgs e) {
			if (!game_over) {
				switch (e.KeyCode) {
					case Keys.W:									// Up.
						if (this.ghost.Top > 0)
							this.ghost.Top -= 8;

						break;

					case Keys.A:									// Left.
						if (this.ghost.Left > 0)
							this.ghost.Left -= 8;
						break;

					case Keys.S:									// Down.
						if (this.ghost.Top < (viewport.Height - this.ghost.Height))
							this.ghost.Top += 8;
						break;

					case Keys.D:									// Right.
						if (this.ghost.Left < (viewport.Width - this.ghost.Width))
							this.ghost.Left += 8;
						break;

					case Keys.Enter:							// Lock!
						UseLock();
						break;

					default:
						break;
				}
			} else {
				DialogResult result = MessageBox.Show(this, "¡Has muerto de un infarto a causa de una sobrecarga de notificaciones!\n\n¿Quieres jugar otra vez?", "Nooo!", MessageBoxButtons.YesNo);

				if (result == System.Windows.Forms.DialogResult.Yes) {
					Application.Restart();
				} else Application.ExitThread();
			}
		}

		private void OnProtectionTimerTick(object sender, EventArgs e) {
			if (is_protected) {
				is_protected = false;
				UpdateLocks();
			}
		}

		private EntityType GetRandomEntityType() {
			return (EntityType)App.GetRandom(0, 5);
		}

		private void SpawnEntity(object sender, EventArgs e) {
			Size entitySize = new Size(24, 24);
			Point initialPosition = new Point(App.GetRandom(0, viewport.Width - entitySize.Width), 24 - entitySize.Height);
			Panel entity = new Panel();
			EntityType entityType = EntityType.Like;

			if (locks > 0)
				entityType = GetRandomEntityType();
			else if (App.GetRandom(0, 16) > 0)
				entityType = EntityType.Lock;

			EntityData data = new EntityData(entityType, (uint)App.GetRandom(5, 10), ((App.GetRandom(0, 3) == 0) ? true : false));

			entity.Size = entitySize;
			entity.Location = initialPosition;
			entity.BackgroundImage = Entity.GetSprite(entityType);
			entity.BackColor = Color.Transparent;
			entity.Tag = data;
			entity.BackgroundImageLayout = ImageLayout.Center;
			((Bitmap)entity.BackgroundImage).MakeTransparent(Color.Black);

			viewport.Controls.Add(entity);
		}

		private void UpdateEntities(object sender, EventArgs e) {
			foreach (Panel entity in viewport.Controls) {
				if (entity.Name != "ghost") {
					try {
						if (entity.Top < viewport.Height)
							entity.Top += (int)((EntityData)entity.Tag).stepSize;
						else
							viewport.Controls.Remove(entity);

						if (((EntityData)entity.Tag).followGhost)
							entity.Left = ghost.Left;

						if (entity.Bounds.IntersectsWith(ghost.Bounds)) {
							viewport.Controls.Remove(entity);

							if (((EntityData)entity.Tag).type == EntityType.Lock) {
								if (locks < 5) {
									locks++;
									UpdateLocks();
								}
							} else {
								

								if (!is_protected) {
									Console.Beep(600, 25);
									Console.Beep(500, 50);
									lives -= 0.5;
								} else {
									lives += 0.25;
									Console.Beep(800, 25);
									Console.Beep(900, 50);
								}
							}

							UpdateLives();
						}

					} catch { }
				}
			}
		}
	}
}
